package com.example.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.daoman.*;
import com.example.mapper.FloorMapper;
import com.example.mapper.UserMapper;
import com.example.service.UserService;
import com.example.utils.GetSession;
import com.example.utils.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserServiceImpl userService;

    @Autowired
    private UperServiceImpl uperService;

    @Autowired
    private PermissionsServiceImpl permissionsService;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private FloorMapper floorMapper;

    //    查询管理员自己管理的楼层的所有学员的信息


    @Override
    public Message getFloorAllUser(List<Floor> floorList) {
        Map<String,Object> map = new HashMap<>();
        ArrayList<Integer> arrayList = new ArrayList<>();
        for (Floor f:floorList){
            arrayList.add(f.getId());
        }
        map.put("fidList",arrayList);
        List<User> allUser = userMapper.getFloorAllUser(map);
        return Message.out(200,"查询",allUser);
    }

    //    用户登录查询
    @Override
    public User login(String name, String password) {
//        查询数据库
        //        密码查询
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        QueryWrapper<User> name1 = queryWrapper.eq("account", name).eq("password", password);
        List<User> list = userService.list(name1);
        if (list.size() == 0) {
            return null;
        }
//        用户存在  查询权限
        User user = list.get(0);
        QueryWrapper<Uper> q = new QueryWrapper<>();
        QueryWrapper<Uper> uid = q.eq("uid", user.getId());
        List<Uper> uperList = uperService.list(uid);
//        查询权限主键
        ArrayList<Integer> pidList = new ArrayList<>();
        for (Uper u : uperList) {
            pidList.add(u.getPid());
        }
        if (pidList.size() == 0) {
//            无权限
            user.setPermissions(null);
        } else {
            //        查询权限
            List<Permissions> permissionsList = permissionsService.listByIds(pidList);
            user.setPermissions(permissionsList);
        }
        return user;
    }

    //    用户忘记密码
    @Override
    public Message forget(User user) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        QueryWrapper<User> phone = queryWrapper.eq("phone", user.getPhone());
        List<User> list = userService.list(phone);
        if (list.size() == 0) {
            return Message.out(200, "未绑定", false);
        } else {
//            查询该用户的账号密码
            User user1 = list.get(0);
            user1.setPassword(user.getPassword());
            boolean b = userService.updateById(user1);
            if (b) {
                return Message.out(200, "成功", user1);
            }
            return Message.out(200, "成功", false);
        }
    }

    //    查询个人信息
    @Override
    public Message getOne() {
        User user = GetSession.get();  //当前用户信息 已经包含了权限信息部分
//        设置查询用户所在的床位 房间 楼层
        if (user == null) {
            return Message.out(404, "不存在该用户", null);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("uid", user.getId());
        User userBack = userMapper.getAllList(map).get(0);
        userBack.setPermissions(user.getPermissions());
        return Message.out(200, "成功", userBack);
    }

    //    查询所有的用户信息 床位 房间 楼层

    @Override
    public Message getUserAll(int page, int pageSize) {
//        首先获取自己所管理的楼层信息
        List<Floor> floorList = floorMapper.getUserFloorById(GetSession.get().getId());
        ArrayList<Integer> arrayList = new ArrayList<>();
        for (Floor f : floorList) {
            arrayList.add(f.getId());
        }
        System.out.println(arrayList);
        Map<String, Object> map = new HashMap<>();
        map.put("fid", arrayList);
        map.put("page", (page - 1) * pageSize);
        map.put("pageSize", page * pageSize);
//        查询用户总条数
        int userNumber = userMapper.getUserNumber();
//        直接调用函数查询操作 然后根据楼层id查询其中的学生信息  动态sql
        List<User> allList = userMapper.getAllList(map);
        Map<String, Object> objectMap = new HashMap<>();
        objectMap.put("total", userNumber);
        objectMap.put("allList", allList);
        return Message.out(200, "成功", objectMap);
    }

    //    管理员搜索功能 按照姓名搜索
    @Override
    public Message inputUserAll(User user) {
        Map<String, Object> map = new HashMap<>();
        map.put("name", user.getName());
        List<User> allList = userMapper.getAllList(map);
        return Message.out(200, "", allList);
    }
    //    管理员密码重置

    @Override
    public Message reset(List<User> userList) {
//        数据处理 前置重置为000000
        for (User u : userList) {
            u.setPassword("000000");
        }
        boolean b = userMapper.updatePassword(userList);
        return Message.out(200, "批量跟新", b);
    }

    //    批量删除所选择的用户

    @Override
    public Message removeUserList(List<User> userList) {
//        获取用户的id
        ArrayList<Integer> arrayList = new ArrayList<>();
        for (User u : userList) {
            arrayList.add(u.getId());
        }
        Map<String, Object> map = new HashMap<>();
        map.put("arrayList", arrayList);
        boolean b = userMapper.removeUser(map);
        return Message.out(200, "成功", b);
    }

//    数据的批量导入


    @Override
    public boolean setUserList(Map<String,Object> map) {
        boolean b = userMapper.setUserList(map);
        return b;
    }
}
